5 CLS:DIMA(64),X,Y,P,A,B,C,D,E,F,H,L,M,Q,R,V,W,Z,CL,TM,A$:X=RND(-(PEEK(9)*256+PEEK(10))):GOTO2500
10 O=0:B=0:A(0)=0:A(25)=0:Q=4
20 DATA 2,0,0,0,0,-5,0,-3,0,0,0,5,-5,0,0,0,3,0,5,0,0,0,0,-2
30 FOR X=1 TO 24:READ A(X):NEXT X
40 GOSUB 1000
60 P=RND(6):C=RND(6)
70 PRINT@0,"YOU";P;
75 PRINT"ME";STR$(C)". ";
80 IFP=CTHEN60
90 IFP>CTHENPRINT"YOU";:GOTO110
100 PRINT"I";
110 INPUT" GO FIRST";A$
120 IFP>CTHEN600
130 REM ME'S TURN
140 T=0:GOSUB 1300
150 GOSUB1350:PRINT" THINKING...";
155 REM ARE ALL PIECES ON INNER BOARD?
160 C=1:FOR Z=0 TO 18:IF A(Z)>0 THEN C=0
170 NEXT Z
171 REM PLAYER HASN'T PASSED COMPUTER?
172 V=0:P=0:FOR Z=1 TO 24:IF (A(Z)<0)*P THEN V=1
174 IF A(Z)>0 THEN P=1
176 NEXT Z
178 H=0:L=0:R=0:FOR Y=0 TO 3:IF A(Y+26)=0 THEN 315
180 FOR X=0 TO 25-A(Y+26):E=0:IF A(X)<=0 THEN 310
190 IF (X=0)*(A(X)>0) THEN E=E+128
200 IF X+A(26+Y)=25 THEN E=E+64
210 IF (A(X)=1)*(A(X+A(26+Y)))*V THEN E=E+16
220 IF (A(X)<>2)*(A(X+A(26+Y)))*V THEN E=E+8
230 IF (A(X+A(26+Y))=-1)+(X<7) THEN E=E+32
240 IF X<19 THEN E=E+4
250 IF (A(X+A(26+Y))<-1)+((C=0)*(X+A(26+Y)>24)) THEN 310
260 E=E+1:IF (X+A(26+Y)=25)+(Y=3) THEN 300
270 FOR Z=0 TO 3:D=X+A(26+Y)-A(26-Z):IF (Y=Z)+(A(26+Z)=0)+(D<0)+(D>24) THEN 290
280 IF (A(D)=1)+(A(D)>2) THEN E=E+2:TM=Z:Z=3:NEXT Z:Z=TM:GOTO 300
290 NEXT Z
300 IF (H<E)+(H=E)*((X<L)+(X=L)*(R<A(26+Y))) THEN H=E:R=A(26+Y):L=X
310 NEXT X
315 NEXT Y
320 IF (H<128)*(A(0)>0) THEN 580
330 IF H<>0 THEN 390
340 R=7:FOR Y=0 TO 3:IF (A(26+Y)<R)*(A(26+Y)>1) THEN R=A(26+Y):L=Y
350 NEXT Y:FOR X=0 TO 25-R:IF A(X)>0 THEN TM=X:X=25-R:NEXT X:X=TM:GOTO 580
360 NEXT X:FOR X=26-R TO 24:IF A(X)>0 THEN A(26+L)=25-X:TM=X:X=24:NEXT X:X=TM:GOTO 178
370 NEXT X:GOTO580
390 IF L>0 THEN C=A(L):P=L:GOSUB1600
400 A(L)=A(L)-1:IF L>0 THEN C=A(L):P=L:GOSUB1280
430 IFL=0THENGOSUB1550
440 IF A(L+R)=-1THEN B=B+1:A(L+R)=0:C=1:P=L+R:GOSUB1600:GOSUB1550
450 Y=A(L+R)+1:A(L+R)=Y:IFL+R<25THENC=A(L+R):P=L+R:GOSUB2000:GOTO453
452 GOSUB1550
453 FOR Z=0 TO 3:IF A(26+Z)<>R THEN NEXT Z
454 TM=Z:Z=3:NEXT Z:Z=TM
456 GOSUB1350:PRINT" FROM";:X=L:GOSUB1850:PRINT" TO";:X=L+R:GOSUB1850:A(26+Z)=0
458 INPUTA$
460 IFA(25)=15 THEN 560
470 TM=0:FORX=0TO3:IFA(26+X)>0THENTM=1
480 NEXTX
490 IFTM=1THEN150
500 GOTO600
560 GOSUB1500:PRINT@0,"I WIN.";:SOUND 200,1:SOUND100,3:GOTO 860
580 GOSUB1350:PRINT" I CAN'T MOVE";
590 INPUTA$
600 REM PLAYER'S TURN
605 IFPEEK(16384+32)<>79THENGOSUB1000
610 T=1:GOSUB1300
620 GOSUB1350:IFBTHENF=25:PRINT" FROM BAR";:GOTO640
630 R=999:N=99:INPUT" FROM";F$:F=INT(VAL(F$))
635 IFF$="N"THEN130
637 IFPEEK(16384+32)<>79THENGOSUB1000:GOTO620
640 IFF$="R" THEN GOSUB 1000:GOTO 620
650 IF((F=25)*(B=0))+(F<1)+(F>25) THEN 980
660 IF(F<25)*(A(F)>=0) THEN 980
670 PRINT@24,"";:X=O:O=0:N=99:R=999:INPUT"TO";D$:O=X:D=INT(VAL(D$))
671 IFPEEK(16384+32)<>79THENGOSUB1000:GOTO620
672 IFD$="N"THEN130
673 IFD$="R"THENGOSUB1000:GOTO620
675 IF(D<0)+(D>24)THEN980
680 IFA(D)>1 THEN 980
682 M=0:G=0:FORW=0TO3:IFA(26+W)=F-DTHEN:TM=W:W=3:NEXTW:W=TM:GOTO690
683 IFM<A(26+W)THENM=A(26+W):G=W
684 NEXTW:IF(D<>0)+(B<>0)THEN980
685 REM DICE EXCEED WHAT'S NEEDED TO MOVE OFF?
686 W=G:IFF<MTHENM=F+1
687 FOR X=M TO 6:IF A(X)<0 THEN TM=X:X=6:NEXT X:X=TM:GOTO 980
688 NEXT X:GOTO706
689 REM PUT COMPUTER ON BAR?
690 IF (D>0)*(A(D)=1) THEN A(0)=A(0)+1:GOSUB 1550:A(D)=0
695 REM FROM BAR?
700 IF F=25 THEN B=B-1:GOSUB 1550:GOTO 730
702 REM FROM NORMAL
704 IF D<>0 THEN 710
706 FOR X=7 TO 24:IF A(X)<0 THEN TM=X:X=24:NEXT X:X=TM:GOTO 980
708 NEXT X
710 C=A(F):P=F:GOSUB1600:A(F)=A(F)+1:C=A(F):P=F:GOSUB1280
720 IF D=0 THEN 800
730 A(D)=A(D)-1:P=D:C=A(D):GOSUB2000:A(26+W)=0
740 REM ANY MORE DICE LEFT?
750 FOR W=0 TO 3:IF A(26+W)<>0 THEN TM=W:W=3:NEXT W:W=TM:GOTO 620
760 NEXT W:GOTO 130
800 REM JUST MOVED OFF
840 O=O+1:GOSUB 1550:A(26+W)=0:IF O<15 THEN 750
850 GOSUB 1500:PRINT@0,"YOU WIN! ";:SOUND 100,1:SOUND200,3
860 PRINT@9,"PLAY AGAIN";:Y=1:N=0:INPUT A$:IF (LEFT$(A$,1)<>"Y") AND (LEFT$(A$,1)<>"N") THEN 860
870 IF LEFT$(A$,1)="Y" THEN RESTORE:GOTO 10
880 END
970 REM ILLEGAL MOVE
980 GOSUB 1500:PRINT@0,"";
990 INPUT"ILLEGAL";A$:GOTO620
999 REM REDRAW SCREEN
1000 CLS0
1003 FOR X=0 TO 31:PRINT" ";:NEXT X
1004 PRINT@1*32, "OFF 242322212019bar 181716151413";
1005 PRINT@15*32,"OFF 1 2 3 4 5 6 bar 7 8 9 10111";:POKE16895,114
1010 PK=16415:FORCL=1TO32:A$=MID$("    24  22  20      18  16  14  ",CL,1):IFA$<>" "THENPOKEPK+CL,ASC(A$)
1020 NEXTCL
1030 PK=16863:FORCL=1TO32:A$=MID$("    1   3   5       7   9   11  ",CL,1):IFA$<>" "THENPOKEPK+CL,ASC(A$)
1040 NEXTCL
1050 FORP=1TO24:X=4*(P-1)+9:IFP>12THENX=4*(24-P)+9
1055 IFP>6ANDP<19THENX=X+8
1060 FORY=5TO14:SET(X,Y,5):NEXTY
1120 FORY=27TO18STEP-1:SET(X,Y,5):NEXTY
1180 NEXTP
1190 GOSUB1550
1200 PRINT@4*32+16,"ñóò";:PRINT@10*32+16,"¡£¢";
1210 PRINT@10*32,"ñóò";:PRINT@4*32,"¡£¢";
1240 FORP=1TO24:S=1:IFA(P)<0THENS=-1
1250 FORC=0TOA(P)STEPS:GOSUB2000:NEXTC:NEXTP
1260 PRINT@0,"";
1270 RETURN
1280 S=1:IFA(P)<0THENS=-1
1290 FORC=0TOA(P)STEPS:GOSUB2000:NEXTC:RETURN
1300 REM ROLL DICE
1310 A(26)=RND(6):A(27)=RND(6):A(28)=0:A(29)=0
1320 IF A(26)=A(27) THEN A(28)=A(26):A(29)=A(26)
1325 FOR X=0 TO 3:A(30+X)=A(26+X):NEXT X
1330 RETURN
1350 REM PRINT DICE
1360 GOSUB1500:IFT=0THENPRINT@2,"ROLL";:CL=3:GOTO1370
1365 PRINT@2,"ROLL";:CL=8
1370 FORX=0TO4:SET(.5*X,1*.66,CL):NEXTX
1375 FORX=26TO29
1380 IFA(X)<>0THENONA(X+4)GOSUB1400,1410,1420,1430,1440,1450
1390 NEXTX:RETURN
1400 PRINT" 1";:RETURN
1410 PRINT" 2";:RETURN
1420 PRINT" 3";:RETURN
1430 PRINT" 4";:RETURN
1440 PRINT" 5";:RETURN
1450 PRINT" 6";:RETURN
1500 REM CLEAR TOP LINE
1510 PRINT@0,"                                ";
1520 RETURN
1550 REM UPDATE BAR/OFF COUNTERS
1560 PRINT@32*5+16,"   ";:PRINT@32*5+16,STR$(B);:PRINT@32*11+16,"   ";:PRINT@32*11+16,STR$(A(0));
1570 PRINT@32*11,"   ";:PRINT@32*11,STR$(O);:PRINT@32*5,"   ";:PRINT@32*5,STR$(A(25));
1580 PRINT@0,"";:RETURN
1600 REM ERASE A PIECE
1610 IFC=0THENRETURN
1620 C=ABS(C):X=4*(P-1)+8:Y=44-C*2
1630 IFP>12THENX=4*(24-P)+8:Y=(6+C*2)*.5+1:GOTO1636
1635 Y=(Y*.5)+6
1636 IFP>6ANDP<19THENX=X+8
1640 IFC>10THENRETURN
1740 FORA=XTOX+2:RESET(A,Y):NEXTA
1750 IFP>12ANDC<10THENSET(X+1,Y+1,5):GOTO1770
1760 IFC<10THENSET(X+1,Y-1,5)
1770 IFC=1THENSET(X+1,Y,5)
1780 RETURN
1850 REM PRINT TWO DIGIT NUMBER
1860 IFX=0THENPRINT" BAR";:RETURN
1865 IFX=25THENPRINT" OFF";:RETURN
1870 PRINTSTR$(X);:RETURN
2000 REM DRAW A PIECE
2010 IFC=0THENRETURN
2030 X=4*(P-1)+8:Y=44-ABS(C)*2
2040 IFP>12THENX=4*(24-P)+8:Y=(6+ABS(C)*2)*.5+1:GOTO2050
2045 Y=(Y*.5)+6
2050 IFP>6ANDP<19THENX=X+8
2055 IFABS(C)>10THENRETURN
2060 CL=3:IFC<0THENCL=8
2061 IFP>12ANDPOINT(X+1,Y+1)=5THENRESET(X+1,Y+1):GOTO2070
2062 IFPOINT(X+1,Y-1)=5THENRESET(X+1,Y-1)
2070 FORA=XTOX+2:SET(A,Y,CL):NEXTA:RETURN
2500 PRINT"BACKGAMMON: BY JEFF VAVASOUR"
2501 PRINT"MC-10 VERSION BY JIM GERRIE":PRINT
2502 PRINT"DO YOU NEED INSTRUCTIONS?"
2510 INPUTA$:IFLEFT$(A$,1)="Y"THEN3000
2520 GOTO10
3000 CLS
3001 PRINT"WHEN ASKED 'FROM?' AND 'TO?'"
3002 PRINT"ENTER A NUMBER FROM 1-24 TO"
3003 PRINT"REPRESENT A POSITION ON THE"
3004 PRINT"BOARD. ANSWER 'N' IF YOU"
3005 PRINT"CANNOT MOVE OR 'O' IN"
3006 PRINT"RESPONSE TO 'TO?' IF YOU WANT"
3007 PRINT"TO MOVE OFF. IF YOU NEED TO"
3008 PRINT"REDRAW THE SCREEN, TYPE 'R'"
3009 PRINT"AT THE 'FROM?' OR 'TO?'"
3010 PRINT"PROMPT.":PRINT
3011 GOSUB4000
3101 PRINT"GENERAL RULES OF BACKGAMMON:"
3102 PRINT
3103 PRINT"TWO DICE ARE ROLLED EACH"
3104 PRINT"TURN. FOR EACH ROLL, YOU CAN"
3105 PRINT"MOVE A PIECE CLOCKWISE AROUND"
3106 PRINT"THE BOARD (I.E. DOWN FROM"
3107 PRINT"POSITION 24 TO POSITION 1)"
3108 PRINT"ACCORDING TO THE ROLL. IF YOU"
3109 PRINT"ROLL DOUBLES, YOU GET TO MOVE"
3110 PRINT"FOUR TIMES INSTEAD OF TWO."
3111 PRINT:GOSUB4000
3151 PRINT"YOU CAN MOVE YOUR PIECE"
3152 PRINT"CLOCKWISE INTO ANY OPEN"
3153 PRINT"POSITION NOT OCCUPIED BY TWO"
3154 PRINT"OR MORE COMPUTER PIECES. IF"
3155 PRINT"THERE IS ONE COMPUTER PIECE"
3156 PRINT"THERE, IT IS SENT TO THE"
3157 PRINT"'BAR'. FROM THE BAR, THE"
3158 PRINT"COMPUTER MUST START AGAIN AT"
3159 PRINT"THE LOWER RIGHT."
3160 PRINT:GOSUB4000
3201 PRINT"THE COMPUTER MOVES ITS PIECES"
3202 PRINT"COUNTER-CLOCKWISE FROM 1 TO"
3203 PRINT"24 AND, IF IT LANDS ON A"
3204 PRINT"POSITION WHERE YOU ONLY HAVE"
3205 PRINT"ONE PIECE, CAN SEND YOUR"
3206 PRINT"PIECE TO THE BAR. IF YOU ARE"
3207 PRINT"ON BAR YOU MUST START AGAIN"
3208 PRINT"FROM THE UPPER LEFT. (FOR THE"
3209 PRINT"PLAYER, BAR IS EQUIVALENT TO"
3210 PRINT"POSITION #25.) PIECES ON BAR"
3211 PRINT"MUST BE MOVED OFF BEFORE ANY"
3212 PRINT"OTHER PIECE CAN BE MOVED."
3213 PRINT:GOSUB4000
3301 PRINT"YOUR GOAL IS TO MOVE ALL YOUR"
3302 PRINT"PIECES INTO YOUR 'INNER"
3303 PRINT"BOARD' WHICH ARE THE"
3304 PRINT"POSITIONS 1-6. ONCE THEY ARE"
3305 PRINT"ALL IN THERE, YOU CAN MOVE"
3306 PRINT"THEM OFF THE BOARD. MOVING"
3307 PRINT"OFF REQUIRES THE DIE ROLL BE"
3308 PRINT"EQUAL TO THE POSITION NUMBER"
3309 PRINT"OF THE PIECE YOU WISH TO MOVE"
3310 PRINT"OFF BOARD. IF YOU ROLL A"
3311 PRINT"NUMBER THAT IS HIGHER THAN"
3312 PRINT"THE POSITION OF ANY REMAINING"
3313 PRINT"PIECE, YOU MAY INSTEAD MOVE"
3314 PRINT"THE FURTHEST PIECE OUT OFF"
3315 PRINT"THE BOARD.":GOSUB4000
3401 PRINT"THE COMPUTER CAN MOVE OFF"
3402 PRINT"BOARD WHEN ALL ITS PIECES ARE"
3403 PRINT"IN ITS INNER BOARD --"
3404 PRINT"POSITIONS 19 THROUGH 24."
3405 PRINT""
3406 PRINT"THE FIRST PLAYER TO MOVE ALL"
3407 PRINT"PIECES OFF THE BOARD WINS."
3408 PRINT:GOSUB4000
3409 GOTO10
4000 PRINT"PRESS ANY KEY TO CONTINUE";
4010 A$=INKEY$:IFA$=""THEN4010
4020 CLS:RETURN
